using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class ScannerItem : BaseItem
	{
		private Transform pole;

		private Vector2 originalModelPos;

		private float originalPoleX;

		private Vector2 lastTouchPoint;

		private float minX = -1f;

		private float maxX;

		private float minY = -0.5f;

		private float maxY = 1.7f;

		private ScannerDisplayerItem displayer;

		private float curX;

		private float curY;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			pole = model.Find("Pole");
			pole.SetLocalZ(1E-05f);
			originalPoleX = pole.localPosition.x;
			originalModelPos = model.localPosition;
			curX = data.modelX;
			curY = data.modelY;
			if (curX != 0f || curY != 0f)
			{
				SetModelPosition(curX, curY);
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			lastTouchPoint = touchEventData.curPos;
			displayer = (ScannerDisplayerItem)Singleton<Scene>.Instance.GetItemByScriptType(72);
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			float x = model.localPosition.x + (touchEventData.curPos.x - lastTouchPoint.x);
			float y = model.localPosition.y + (touchEventData.curPos.y - lastTouchPoint.y);
			SetModelPosition(x, y);
			lastTouchPoint = touchEventData.curPos;
		}

		private void SetModelPosition(float x, float y)
		{
			curX = x;
			curY = y;
			if (curX < originalModelPos.x + minX)
			{
				curX = originalModelPos.x + minX;
			}
			else if (curX > originalModelPos.x + maxX)
			{
				curX = originalModelPos.x + maxX;
			}
			if (curY < originalModelPos.y + minY)
			{
				curY = originalModelPos.y + minY;
			}
			else if (curY > originalModelPos.y + maxY)
			{
				curY = originalModelPos.y + maxY;
			}
			model.localPosition = new Vector3(curX, curY, 0f);
			float num = curX - originalModelPos.x;
			pole.SetLocalX(originalPoleX - num);
			if (displayer != null)
			{
				float y2 = curY - originalModelPos.y;
				displayer.SetRelativePosition(num, y2);
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["34"] = curX.ToString("F3");
			obj["39"] = curY.ToString("F3");
		}
	}
}
